//
//  SubmitBtnView.m
//  CloudTest
//
//  Created by 张帅 on 2020/7/27.
//  Copyright © 2020 张帅. All rights reserved.
//

#import "SubmitBtnView.h"
@interface SubmitBtnView ()
@property(nonatomic,strong)UIButton * btn;
@property(nonatomic,strong)UILabel * showLabel;
@property(nonatomic,strong)UIImageView  * imageView;
@property(nonatomic,strong)UIView * baseView;
@end
@implementation SubmitBtnView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self uiSet];
    }
    return self;
}
- (void)uiSet {
    self.layer.cornerRadius = 22;
    self.layer.masksToBounds = YES;
    
    self.showLabel = [UIFactory create_LabelContent:nil textColor:[UIColor whiteColor] fontSize:16];
    self.showLabel.textAlignment = NSTextAlignmentCenter;
    self.showLabel.frame = self.bounds;
    [self addSubview:self.showLabel];
    
    self.btn = [UIFactory create_btnWithTitle:@"" imageName:nil backgroundColor:nil TypefaceColor:theme_color fontSize:14 target:self action:@selector(btnClicked)];
    self.btn.frame = self.bounds;
    [self addSubview:self.btn];
    
    [self addBaseView];
    
}
- (void)btnClicked{
    if (self.btnClick_block) {
        self.btnClick_block();
    }
}
- (void)setShowType:(SubmitBtnViewShowType)showType {
    _showType = showType;
    if (_showType == Show_simulation_test_type) {//模拟考试显示
        self.showLabel.text = @"正式考试";
        self.backgroundColor = theme_color;
    } else if (_showType == Show_formal_test_not_submit_type) {//正式考试不可点击状态
        self.showLabel.text = @"视频上传";
        self.backgroundColor = color(@"#E5E5E5");
    } else if (_showType == Show_formal_test_submit_type) {//正式考试可点击状态
        self.showLabel.text = @"视频上传";
        self.backgroundColor = theme_color;
    } else if (_showType == Show_Uploading_type) {//上传中状态
        self.showLabel.text = @"上传中";
        self.backgroundColor = color(@"#6695FF");
    } else if (_showType == Show_retry_submit_type) {//重新上传
        self.showLabel.text = @"重新上传";
        self.backgroundColor = theme_color;
    }
    
    if (_showType == Show_Uploading_type) {
        self.baseView.hidden = NO;
        [self kaisi];
    } else {
        self.baseView.hidden = YES;
        [self.imageView.layer removeAnimationForKey:@"animation"];
    }
    
    if (_showType == Show_formal_test_not_submit_type || _showType == Show_Uploading_type) {//bu
        self.btn.enabled = NO;
    } else {
        self.btn.enabled = YES;
    }
}
- (void)addBaseView {
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    self.baseView.backgroundColor = color(@"#6695FF");
    [self addSubview:self.baseView];
   
    self.imageView = [UIFactory create_ImageView_ImageName:@"圆圈动画"];
    self.imageView.frame = CGRectMake(0, 0, 17, 17);
    self.imageView.centerY = self.baseView.centerY;
    [self.baseView addSubview:self.imageView];
    
    UILabel * label = [UIFactory create_LabelContent:@"上传中" textColor:[UIColor whiteColor] fontSize:16];
    label.frame=CGRectMake(self.imageView.right+7.5, 0, 100, self.baseView.height);
    [self.baseView addSubview:label];
    CGSize size = [label sizeThatFits:CGSizeMake(100, self.baseView.height)];
    label.width = size.width;
    self.baseView.width = self.imageView.width + 7.5 + label.width;
    self.baseView.left = (self.width/2) - (self.baseView.width/2);
    
}
- (void)kaisi {
    CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath: @"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //围绕Z轴旋转，垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI/2.0, 0.0, 0.0, 1.0) ];
    animation.duration = 0.1;
    //旋转效果累计，先转180度，接着再旋转180度，从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    [self.imageView.layer addAnimation:animation forKey:@"animation"];
    

}
@end
